Repository navigation
Delete Z3's unpacked native libraries once they are loaded - #541
Merged
Merged
Conversation
Collaborator
Author
|
Codex review, round 1 Prompt: review Result:
Nothing to fix, so the Codex loop stops after this round. |
Collaborator
Author
|
Clean-code review (clean-code-principles skill, only the code this PR adds) Fixed (f707a83):
Declined:
|
z3-turnkey unpacks libz3 into a fresh temp folder per JVM and removes it only on a normal exit, so every killed verifier leaked about 30 MB. Once the first Z3 context exists, delete the files this process has mapped (read from /proc/self/maps) and their folder; on Linux a loaded library stays usable after its file is deleted. Elsewhere nothing changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CatarinaGamboa
force-pushed
the
fix/535-z3-temp-leak
branch
from
November 23, 2026 16:46
f707a83 to
8c38c6d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #535
Cause
tools.aqua:z3-turnkey4.8.17 loads Z3 in the static initializer ofcom.microsoft.z3.Native, which callsZ3Loader.loadZ3()without any condition. That method makes a fresh${java.io.tmpdir}/z3-turnkey<random>folder, unpackslibz3.soandlibz3java.sointo it (about 30 MB), marks themdeleteOnExitandSystem.loads them.deleteOnExitonly runs on a normal JVM exit, so every verifier JVM that is killed (language server, MCP server, timed-out test run) leaves its folder behind.Fix
Once Z3 is loaded, the unpacked files are no longer needed: on Linux a loaded library stays mapped and usable after its file is deleted. The first
TranslatorToZ3(the only place that creates a Z3Context) now callsZ3NativeLibraries.deleteUnpackedFiles(). It reads/proc/self/maps, takes the mapped files whose folder starts withz3-turnkey, deletes them and then their folder. The z3-turnkeydeleteOnExithooks later find nothing to delete, which is harmless.It only deletes files that this process has mapped, so the folder of another running verifier is never touched, and there is no race with JVMs that are still unpacking. If
/proc/self/mapsis missing (macOS, Windows) or a delete fails, nothing changes: the folder is still removed at a normal exit, as today. The only window left on Linux is the few milliseconds between unpacking and the firstTranslatorToZ3.Alternatives considered
tools.aqua:turnkey-support1.0.0 (the only version), and itsTurnKeyloader still usesFiles.createTempDirectory+deleteOnExit. A bump would change solver behaviour and not fix the leak.System.load, delete). Not enough on its own:Native's static initializer always callsZ3Loader.loadZ3(), and there is no system property to turn it off, so z3-turnkey would still unpack (and leak) a second copy. Avoiding that would mean shadowingcom.microsoft.z3.Z3Loaderwith our own class on the classpath, which depends on classpath order and is fragile.java.io.tmpdirjust for the load. Not possible: the JDK reads the temp directory once at startup.z3-turnkey*folders at startup. Not done. With several verifiers running at once, it is hard to tell a leftover from a folder another JVM is unpacking right now. With this fix, Linux JVMs no longer leave folders, so there is little left to sweep. Folders that leaked before this fix still need a one-timerm -rf /tmp/z3-turnkey*while no verifier runs.Verification
CommandLineLauncheronliquidjava-example/src/main/java/testSuitewith a private-Djava.io.tmpdir, waited until Z3 was mapped and solving, thenkill -9:.../z3-turnkey2988981119362764953/libz3.so (deleted)and keeps verifying; after the kill the tmpdir is empty (4 KB).z3-turnkey12893544316004404040/withlibz3.so+libz3java.sois left behind (30 MB).mvn -q testfrom the repo root passes, and leaves nothing in its private tmpdir.🤖 Generated with Claude Code